home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / CNews / Source / misc / getabsdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  1.4 KB  |  87 lines

  1. /*
  2.  * getabsdate absolute_date ... - convert absolute_date to seconds since epoch
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <sys/types.h>
  9. #include <sys/timeb.h>
  10.  
  11. /* privates */
  12. static struct timeb ftnow;
  13. static int exitstatus = 0;
  14.  
  15. char *progname;
  16.  
  17. /* imports */
  18. extern long atol();
  19. extern char *malloc();
  20. extern struct tm *gmtime();
  21. extern time_t time();
  22. extern int optind;
  23. extern char *optarg;
  24.  
  25. extern char *strsave();
  26. extern time_t getabsdate();
  27.  
  28. /* Forwards. */
  29. extern void process();
  30.  
  31. /*
  32.  - main - parse arguments and handle options
  33.  */
  34. main(argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.     register int c, errflg = 0;
  39.  
  40.     progname = argv[0];
  41.     ftime(&ftnow);
  42.  
  43.     while ((c = getopt(argc, argv, "")) != EOF)
  44.         switch (c) {
  45.         case '?':
  46.         default:
  47.             errflg++;
  48.             break;
  49.         }
  50.     if (errflg || optind == argc) {
  51.         (void) fprintf(stderr, "Usage: %s ascii_time ...\n", progname);
  52.         exit(2);
  53.     }
  54.  
  55.     for (; optind < argc; optind++)
  56.         process(argv[optind]);
  57.     exit(exitstatus);
  58. }
  59.  
  60. /*
  61.  * process - print time_t of tm
  62.  */
  63. void
  64. process(timestr)
  65. char *timestr;
  66. {
  67.     register time_t tstime;
  68.     register char *copy = strsave(timestr);
  69.  
  70.     if (copy == NULL) {
  71.         exitstatus = 1;
  72.         return;
  73.     }
  74.     tstime = getabsdate(copy, &ftnow);
  75.     if (tstime < 0) {
  76.         (void) fprintf(stderr, "%s: `%s' not a valid date\n",
  77.                    progname, timestr);
  78.         exitstatus = 1;
  79.     } else
  80.         (void) printf("%ld\n", tstime);
  81.     free(copy);
  82. }
  83.  
  84. unprivileged()            /* strsave requires this; ugh */
  85. {
  86. }
  87.